﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Data;

namespace WebApplication1
{
    // ActiveOrder Page is for cafetaria
    public partial class ActiveOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            fos_orderdetails a = new fos_orderdetails();


         //   Authorization();
        }


        public void Authorization()
        {
            Business.User userobj = (Business.User)Session["User"];
            // Default admin links and cafe links are disable
            List<fos_UserLinkRole> userRoles = new List<fos_UserLinkRole>();
            // Role Id 1 is admin, Role ID 3 is Cafetaria, Role Id 2 is Employee
            int adminCount = userRoles.Count(a => a.RoleId == 1);
            int CafeCount = userRoles.Count(a => a.RoleId == 2);
            // Not application
            //if (adminCount < 1)
            //    throw new Exception("Unauthorized access!");
            if (CafeCount < 1)
                throw new Exception("Unauthorized access!");
        }

        protected void odsGetFoodDetailsByCaterer_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {

            Business.User userobj = (User)Session["User"];
            if (userobj != null)
            {
                e.InputParameters["email"] = userobj.email;

            }
        }

        protected void chkboxPick_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                CheckBox chkbox = (CheckBox)sender;
                long orderdetailsId = Convert.ToInt64(chkbox.InputAttributes["value"]);
                Order myorder = new Order();
                myorder.UpdateOrderDetailPickup(orderdetailsId, chkbox.Checked);
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-success  alert-dismissible";
                lblMessage.Text = "Order updated!";
            }
            catch (Exception ex)
            {
                divMessage.Visible = true;
                divMessage.CssClass = "alert alert-danger  alert-dismissible";
                lblMessage.Text = ex.Message;

            }
        }

        protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem dataItem = (ListViewDataItem)e.Item;
                long orderdetailID = ((Business.fos_orderdetails)dataItem.DataItem).Id;

                var chk = e.Item.FindControl("chkboxPick") as CheckBox;
              
                if (chk != null)
                {

                    chk.InputAttributes.Add("value", orderdetailID.ToString());
                }

            }
        }


    }
}